package com.senthink.gl.business.userlog;

import com.senthink.gl.business.common.entity.TimeSearchRequest;
import com.senthink.gl.business.userlog.enums.LogOperateEnum;
import com.senthink.gl.business.common.entity.PageDto;
import com.senthink.gl.business.common.entity.SearchRequest;
import com.senthink.gl.business.domain.entity.User;
import com.senthink.gl.business.domain.entity.UserLog;
import com.senthink.gl.business.domain.service.UserService;
import com.senthink.gl.business.messages.CodeMsg;
import com.senthink.gl.business.response.ApiResponse;
import com.senthink.gl.business.response.CodeMsgDataResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yuanpeng@senthink.com
 * @data 2018-02-01 14:02
 */

@Service
public class UserLogService {
    @Autowired
    private CodeMsg codeMsg;
    @Autowired
    private UserLogDataService dataService;
    @Autowired
    private UserService userService;

    /**
     * 获取操作日志分页列表
     *
     * @param role          角色
     * @param operate       操作类型
     * @param timeSearchRequest 时间搜索工具类
     * @param searchRequest 模糊搜索值域
     * @param pageDto       分页工具类
     * @return
     */
    public ApiResponse findAll(String role, String operate, TimeSearchRequest timeSearchRequest, SearchRequest searchRequest, PageDto pageDto) {
        Date startTime;
        Date endTime;
        if (ObjectUtils.allNotNull(timeSearchRequest.getStartTime(), timeSearchRequest.getEndTime())) {
            endTime = timeSearchRequest.getEndTime();
            startTime = timeSearchRequest.getStartTime();
        } else {
            endTime = new Date();
            startTime = new Date(0);
        }
        Page<UserLog> page = dataService.findAll(role, operate,  startTime,  endTime, searchRequest, pageDto.convertToPageRequestOrderByTime());
        List<UserLogResponse> list = new LinkedList<>();
        for (UserLog userLog : page) {
            UserLogResponse response = new UserLogResponse();
            BeanUtils.copyProperties(userLog, response);
            User user = userService.findByUid(userLog.getUid())
                    .orElse(null);
            if (user != null) {
                response.setUsername(user.getName());
                response.setRole(user.getRole());
            } else {
                response.setUsername("unknown");
                response.setRole("unknown");
            }
            list.add(response);
        }
        Page<UserLogResponse> responses = new PageImpl<>(list, pageDto.convertToPageableOrderByTime(), page.getTotalElements());
        return new CodeMsgDataResponse<>(codeMsg.successCode(), codeMsg.successMsg(), responses);
    }

    /**
     * 获取操作类型列表
     *
     * @return
     */
    public ApiResponse operate() {
        return new CodeMsgDataResponse<>(codeMsg.successCode(), codeMsg.successMsg(), LogOperateEnum.set);
    }

}
